home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Interface / AboutBox.c next >
Text File  |  1996-04-12  |  7KB  |  330 lines

  1. /*****
  2.  *
  3.  *    AboutBox.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/mac/grantscgi/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompileWithForeground
  18.  
  19. #include "compiler_stuff.h"
  20.  
  21. /* InternetConfig is not supported with Symantec PPC compiler, yet */
  22. #if __SC__
  23.     #ifdef kCompileWithAboutURL
  24.         #undef kCompileWithAboutURL
  25.         #define kCompileWithAboutURL    0
  26.     #endif
  27. #endif
  28.  
  29. #if kCompileWithAboutURL
  30. #include <ICAPI.h>
  31. #endif
  32.  
  33. #include "constants.h"
  34. #include "globals.h"
  35.  
  36. #include "DebugUtil.h"
  37. #include "IconUtil.h"
  38. #include "StringUtil.h"
  39. #include "WindowInt.h"
  40.  
  41. #include "AboutBox.h"
  42.  
  43.  
  44. /***  LOCAL VARIABLES ***/
  45.  
  46. static    DialogPtr        vAboutBoxDialog;
  47. static    Handle            vAboutIconSuiteHdl;
  48. static    UserItemUPP        vAboutIconUPP;
  49. #if kCompileWithAboutURL
  50. static    UserItemUPP        vAboutURLUPP;
  51. static    Str255            vAboutBoxURL;
  52. #endif
  53.  
  54. /***  LOCAL CONSTANTS ***/
  55.  
  56. #define kriAboutBoxAppIcon        2
  57. #define kriAboutBoxURL            3
  58.  
  59.  
  60. /***  LOCAL PROTOTYPES ***/
  61.  
  62. pascal void        aboutBoxAppIcon    ( DialogPtr, short );
  63. #if kCompileWithAboutURL
  64. pascal void        aboutBoxURL        ( DialogPtr, short );
  65.     ICError        URLDownload        ( StringPtr );
  66. #endif
  67.  
  68.  
  69. /***  FUNCTIONS  ***/
  70.  
  71. #pragma segment Startup
  72. /* initialize the variables used for the about box */
  73. void
  74. AboutBoxInit ( void )
  75. {
  76.     vAboutBoxDialog        = NULL;
  77.     vAboutIconSuiteHdl    = NULL;
  78.     vAboutIconUPP        = NULL;
  79.     
  80.     #if kCompileWithAboutURL
  81.     vAboutURLUPP = NULL;
  82.     GetIndString ( vAboutBoxURL, krURLStrings, 1 );
  83.     #endif
  84. } /* AboutBoxInit */
  85. #pragma segment Utilities
  86.  
  87.  
  88. /* open the about box dialog window */ 
  89. void
  90. AboutBoxOpen ( void )
  91. {
  92.     short            itemType;
  93.     Handle            item;
  94.     Rect            box;
  95.     GrafPtr            savePort;
  96.     
  97.     if ( vAboutBoxDialog == NULL )
  98.     {
  99.         vAboutBoxDialog = GetNewDialog ( kAboutBoxDLOG, NULL, (WindowPtr)-1L );
  100.         
  101.         if ( vAboutBoxDialog != nil )
  102.         {
  103.             WindowNewSetInfo ( vAboutBoxDialog, Window_about, false, nil );
  104.             
  105.             if ( vAboutIconUPP == NULL )
  106.             {
  107.                 vAboutIconUPP = NewUserItemProc ( aboutBoxAppIcon );
  108.             }
  109.             /* install the drawing procedure for the application icon user item */
  110.             GetDialogItem ( vAboutBoxDialog, kriAboutBoxAppIcon, &itemType, &item, &box );
  111.             SetDialogItem ( vAboutBoxDialog, kriAboutBoxAppIcon, itemType, (Handle)vAboutIconUPP, &box );
  112.             
  113.             #if kCompileWithAboutURL
  114.             if ( vAboutURLUPP == NULL )
  115.             {
  116.                 vAboutURLUPP = NewUserItemProc ( aboutBoxURL );
  117.             }
  118.             /* install the drawing procedure for the application icon user item */
  119.             GetDialogItem ( vAboutBoxDialog, kriAboutBoxURL, &itemType, &item, &box );
  120.             SetDialogItem ( vAboutBoxDialog, kriAboutBoxURL, itemType, (Handle)vAboutURLUPP, &box );
  121.             #endif /* kCompileWithAboutURL */
  122.         }
  123.     
  124.         if ( vAboutBoxDialog != NULL )
  125.         {
  126.             /* set the ^0 parameter to be the version string */
  127.             ParamText ( gVersionStr, NULL, NULL, NULL );
  128.             
  129.             /* change the dialog port font to Geneva */
  130.             GetPort        ( &savePort );
  131.             SetPort        ( (GrafPtr)vAboutBoxDialog );
  132.             TextFont    ( geneva );
  133.             SetPort        ( savePort );
  134.         }
  135.     }
  136.     
  137.     /* display the dialog window and bring it to front */
  138.     if ( vAboutBoxDialog != NULL )
  139.     {
  140.         ShowWindow        ( vAboutBoxDialog );
  141.         SelectWindow    ( vAboutBoxDialog );
  142.     }
  143. } /* AboutBoxOpen */
  144.  
  145.  
  146. /* update the contents of the about box dialog window */
  147. void
  148. AboutBoxUpdate ( void )
  149. {
  150.     my_assert ( vAboutBoxDialog != NULL, "\pAboutBoxUpdate: vAboutBoxDialog is NULL" );
  151.     
  152.     /* set the dialog strings */
  153.     ParamText ( gVersionStr, NULL, NULL, NULL );
  154.     
  155.     /* draw the dialog */
  156.     DrawDialog ( vAboutBoxDialog );
  157. } /* AboutBoxUpdate */
  158.  
  159.  
  160. /* close the about box window */
  161. void
  162. AboutBoxClose ( void )
  163. {
  164.     windowInfoHdl    dlogWindInfo;
  165.     
  166.     my_assert ( vAboutBoxDialog != NULL, "\pAboutBoxClose: vAboutBoxDialog is NULL" );
  167.     
  168.     dlogWindInfo = (windowInfoHdl) GetWRefCon ( (WindowRef)vAboutBoxDialog );
  169.     
  170.     if ( dlogWindInfo != NULL )
  171.     {
  172.         DisposeHandle ( (Handle)dlogWindInfo );
  173.     }
  174.     
  175.     DisposeDialog ( vAboutBoxDialog );
  176.     
  177.     vAboutBoxDialog = NULL;
  178. } /* AboutBoxClose */
  179.  
  180.  
  181. /* Handle events in the about box */
  182. Boolean
  183. AboutHandleEvent ( EventRecord *theEvent, DialogPtr theDialog, short itemHit )
  184. {
  185.     if ( theDialog == vAboutBoxDialog )
  186.     {
  187.         /* if it's the about box dialog, handle the event */
  188.         switch ( theEvent->what ) 
  189.         {
  190.             case mouseDown:    
  191.                 switch ( itemHit )
  192.                 {
  193.                     #if kCompileWithAboutURL
  194.                     case kriAboutBoxURL :
  195.                         URLDownload ( vAboutBoxURL );
  196.                         break;
  197.                     #endif
  198.                 }
  199.                 break;
  200.             
  201.             case mouseUp:
  202.                 /* don't do anything */
  203.                 break;
  204.         }
  205.         
  206.         return true;
  207.     }
  208.     else
  209.     {
  210.         return false;
  211.     }
  212. } /*  */
  213.  
  214.  
  215. /* Draw the application icon using the ICN# or a color icon resource */
  216. pascal void
  217. aboutBoxAppIcon ( DialogPtr theDialog, short theItem )
  218. {
  219.     short        itemType;
  220.     Rect        itemRect;
  221.     Handle        itemHandle;
  222.     PenState    curPen;
  223.     WindowPtr    oldPort;
  224.     
  225.     GetDItem ( theDialog, theItem, &itemType, &itemHandle, &itemRect );
  226.     
  227.     GetPort ( &oldPort );
  228.     SetPort ( (GrafPtr)theDialog );
  229.     
  230.     GetPenState    ( &curPen );
  231.     PenNormal    ();
  232.     
  233.     IconDrawFromFamily ( kApplicationIconSuite, &itemRect, &vAboutIconSuiteHdl );
  234.  
  235.     SetPenState    ( &curPen );
  236.     SetPort        ( oldPort );
  237. } /* aboutBoxAppIcon */
  238.  
  239.  
  240. #pragma mark -
  241. #if kCompileWithAboutURL
  242.  
  243. /* Draw the URL string */
  244. pascal void
  245. aboutBoxURL ( DialogPtr theDialog, short theItem )
  246. {
  247.     short        itemType;
  248.     Rect        itemRect;
  249.     Handle        itemHandle;
  250.     PenState    curPen;
  251.     WindowPtr    oldPort;
  252.     short        oldFont;
  253.     short        oldSize;
  254.     short        oldFace;
  255.     RGBColor    oldColor;
  256.     RGBColor    blueColor;
  257.     
  258.     GetDItem ( theDialog, theItem, &itemType, &itemHandle, &itemRect );
  259.     
  260.     GetPort ( &oldPort );
  261.     SetGrafPortOfDialog ( theDialog );
  262.     oldFont = oldPort->txFont;
  263.     oldSize = oldPort->txSize;
  264.     oldFace = oldPort->txFace;
  265.     GetForeColor ( &oldColor );
  266.     
  267.     GetPenState    ( &curPen );
  268.     PenNormal    ();
  269.     
  270.     /* set the text format */
  271.     TextFont ( geneva );
  272.     TextSize ( 9 );
  273.     TextFace ( underline );
  274.     
  275.     blueColor.red = nil;
  276.     blueColor.green = nil;
  277.     blueColor.blue = 0xFFFF;
  278.     RGBForeColor ( &blueColor );
  279.     
  280.     /* draw the string */
  281.     StringDrawInRect ( vAboutBoxURL, itemRect );
  282.     
  283.     RGBForeColor ( &oldColor );
  284.     SetPenState    ( &curPen );
  285.     TextFace    ( oldFace );
  286.     TextSize    ( oldSize );
  287.     TextFont    ( oldFont );
  288.     SetPort        ( oldPort );
  289. } /* aboutBoxURL */
  290.  
  291.  
  292. /* Note that this routine can only succeed if Internet Config 1.1 (or better) is
  293.     installed on this Mac. Version 1.0 does not support the ICLaunchURL call. */
  294. ICError
  295. URLDownload ( StringPtr theURL )
  296. {
  297.     ICError        theErr;
  298.     ICInstance    ICInst;
  299.     long        SelStart;
  300.     long        SelEnd;
  301.     
  302.     theErr = ICStart ( &ICInst, kMyCreatorType );
  303.     if ( theErr == noErr )
  304.     {
  305.         theErr = ICFindConfigFile ( ICInst, nil, NULL );
  306.         if ( theErr == noErr )
  307.         {
  308.             SelStart = nil;
  309.             SelEnd   = *theURL;
  310.             
  311.             theErr = ICLaunchURL ( ICInst, "\p", (Ptr)(theURL + 1), *theURL, &SelStart, &SelEnd );
  312.         }
  313.         
  314.         ICStop ( ICInst );
  315.     }
  316.     
  317.     if ( theErr != noErr )
  318.     {
  319.         SysBeep ( 1 );
  320.     }
  321.     
  322.     return theErr;
  323. } /* URLDownload */
  324.  
  325. #endif /* kCompileWithAboutURL */
  326.  
  327. #endif    /* kCompileWithForeground */
  328.  
  329. /*****  EOF  *****/
  330.